import face_recognition
from PIL import Image, ImageDraw


# 人脸位置
def face_locations(img):
    image = face_recognition.load_image_file(img)
    return image, face_recognition.face_locations(image)


# 人脸关键点
def face_landmarks(img):
    image = face_recognition.load_image_file(img)
    return image, face_recognition.face_landmarks(image)


# 打印关键点
def find_facial_features(img):
    res = face_landmarks(img)
    face_landmarks_list = res[1]
    image = res[0]
    print("I found {} face(s) in this photograph.".format(len(face_landmarks_list)))
    # Create a PIL imagedraw object so we can draw on the picture
    pil_image = Image.fromarray(image)
    d = ImageDraw.Draw(pil_image)

    for landmarks in face_landmarks_list:

        # Print the location of each facial feature in this image
        for facial_feature in landmarks.keys():
            print("The {} in this face has the following points: {}".format(facial_feature,
                                                                            landmarks[facial_feature]))

        # Let's trace out each facial feature in the image with a line!
        for facial_feature in landmarks.keys():
            d.line(landmarks[facial_feature], width=5)
    # Show the picture
    pil_image.show()


# 涂口红
def digital_makeup(img):
    res = face_landmarks(img)
    face_landmarks_list = res[1]
    image = res[0]
    pil_image = Image.fromarray(image)
    for landmarks in face_landmarks_list:
        d = ImageDraw.Draw(pil_image, 'RGBA')
        # Make the eyebrows into a nightmare
        d.polygon(landmarks['left_eyebrow'], fill=(68, 54, 39, 128))
        d.polygon(landmarks['right_eyebrow'], fill=(68, 54, 39, 128))
        d.line(landmarks['left_eyebrow'], fill=(68, 54, 39, 150), width=5)
        d.line(landmarks['right_eyebrow'], fill=(68, 54, 39, 150), width=5)
        # Gloss the lips
        d.polygon(landmarks['top_lip'], fill=(150, 0, 0, 128))
        d.polygon(landmarks['bottom_lip'], fill=(150, 0, 0, 128))
        d.line(landmarks['top_lip'], fill=(150, 0, 0, 64), width=8)
        d.line(landmarks['bottom_lip'], fill=(150, 0, 0, 64), width=8)
        # Sparkle the eyes
        d.polygon(landmarks['left_eye'], fill=(255, 255, 255, 30))
        d.polygon(landmarks['right_eye'], fill=(255, 255, 255, 30))
        # Apply some eyeliner
        d.line(landmarks['left_eye'] + [landmarks['left_eye'][0]], fill=(0, 0, 0, 110), width=6)
        d.line(landmarks['right_eye'] + [landmarks['right_eye'][0]], fill=(0, 0, 0, 110), width=6)
    pil_image.show()


# 人脸裁剪
def face_cut(img):
    res = face_locations(img)
    locations = res[1]
    image = res[0]
    for face_location in locations:
        # 打印每张脸的位置信息
        top, right, bottom, left = face_location
        print(
            "A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom,
                                                                                                  right))
        # 指定人脸的位置信息，然后显示人脸图片
        face_image = image[top:bottom, left:right]
        pil_image = Image.fromarray(face_image)
        pil_image.show()


if __name__ == '__main__':
    find_facial_features('./pic/face1.jpg')
